//
//  EmailViewController.m
//  Email
//
//  Created by Ajay Gautam on 2/22/09.
//

#import "EmailViewController.h"
#import "NSData+Base64Additions.h" // from skpsmtpmessage

@implementation EmailViewController


@synthesize to, content;

- (void)dealloc {
	[to release];
	[content release];
    [super dealloc];
}

- (void)viewDidLoad {
	content.font = [UIFont fontWithName:content.font.fontName size:(content.font.pointSize * (2.0 / 3))];
	content.text = @"<html><head><title>Email</title></head><body><p><div style=\"float:left;\"><img src=\"http://www.ajaygautam.com/00_save/peng_golf.png\"/></div><div>I am testing this out. Hopefully, this will be an html email. Let see</div></p><p><a href=\"http://www.ajaygautam.com/\"><img style=\"border: none;\" src=\"http://www.ajaygautam.com/00_save/visit-site.png\"/></a></p><p><img src=\"http://www.ajaygautam.com/00_save/action.png\"/></p><hr><p>This message has been brought to you by <a href=\"http://www.ajaygautam.com/\"><img style=\"border: none;\" src=\"http://www.ajaygautam.com/00_save/star_yellow.png\"/></a>. Please visit them now</p><hr></body></html>";
    [super viewDidLoad];
}

-(IBAction)doneTyping:(id)sender {
	[sender resignFirstResponder];
}

-(IBAction)emailImage:(id)sender {
	[to resignFirstResponder];
	[content resignFirstResponder];
    
//	NSString *sendTo = [to.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
//    UIImage *img = [UIImage imageNamed:@"beans.jpg"];
    
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"beans.jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    if (!data) {
        NSLog(@"Image file not found!");
        return;
    }
    
    NSString *subject = @"Testing sending image by email";
    NSString *encodedImage = [data encodeBase64ForData];
    
    NSLog(@"Encoded Image: ||%@||", encodedImage);
    
    NSString *body = [NSString stringWithFormat:@"<html><body>Header<hr><img src=\"data:image/jpg;base64,%@\" alt=\"This image is not viewable in your email client\"><hr>Footer</body></html>", encodedImage];
    NSString *completeMessage = [[[@"mailto:?subject=" stringByAppendingString:subject] stringByAppendingString:@"&body="] stringByAppendingString:body];
    NSString *encodedMessage = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)completeMessage, NULL, NULL, kCFStringEncodingUTF8);
    
#if TARGET_IPHONE_SIMULATOR
    [[[[UIAlertView alloc] initWithTitle:@"" message:@"Emails don't work on simulator. Please test on device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
    return;
#endif;
    // send the message to Mail app
    
    NSLog(@"Encoded msg: %@", encodedMessage);
    
    NSURL *url = [NSURL URLWithString:encodedMessage];
    [[UIApplication sharedApplication] openURL:url]; //open Mail.app
    
    /*
    The kMyEncodedImage string looks like this (but I removed a lot from the
                                                gibberish string)
    
    @"<hr><img src=\" alt=\""; 
    */
}

-(IBAction)sendEmail:(id)sender {
	[to resignFirstResponder];
	[content resignFirstResponder];

	NSString *subject = [@"Test Subject" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSString *body = [content.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSString *sendTo = [to.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	NSString *emailUrlStr = [NSString stringWithFormat:@"mailto:%@?Subject=%@&Body=%@", sendTo, subject, body];
	
    NSURL *url = [NSURL URLWithString:emailUrlStr];
    [[UIApplication sharedApplication] openURL:url];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

@end
